package com.aferzsoft.colorslide.classes;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.view.MotionEvent;

public class Cell implements GameInterface{
	
	public final static float CELL_MARGIN 		= 15;
	public final static float CELL_SIZE 		= 80;
	
	private float posX;
	private float posY;
	private Tile tile;
	
	private Paint paint; 
	private RectF drawableRect; 
	
	public Cell( float left, float top ){
		this.posX = left;
		this.posY = top;
		this.paint = new Paint();
		this.drawableRect = new RectF( ( left + CELL_MARGIN ), ( top + CELL_MARGIN ), ( left + CELL_SIZE + CELL_MARGIN * 2 ), ( top + CELL_SIZE + CELL_MARGIN * 2 ) );
	}

	public Cell( float left, float top, Tile tile ){
		this.posX = left;
		this.posY = top;
		this.tile = tile;
		this.paint = new Paint();
		this.drawableRect = new RectF( ( left + CELL_MARGIN ), ( top + CELL_MARGIN ), ( left + CELL_SIZE + CELL_MARGIN * 2 ), ( top + CELL_SIZE + CELL_MARGIN * 2 ) );
	}
	
	public Tile getTile(){
		return this.tile;
	}
	
	public void setTile( Tile tile ){
		this.tile = tile;
	}
	
	public float getPosX() {
		return posX;
	}

	public void setPosX(int posX) {
		this.posX = posX;
	}
	
	public float getPosY() {
		return posY;
	}
	
	public void setPosY(int posY) {
		this.posY = posY;
	}
	
	public boolean tileExists(){
		if( this.tile == null )
			return false;
		else
			return true;
	}
	
	@Override
	public boolean onDraw(Canvas canvas) {
		paint.setColor( this.tile.getColor() );
		paint.setStyle(Style.FILL);
		canvas.drawRoundRect( this.drawableRect, 15, 15, paint);
		paint.setColor( Color.BLACK );
		paint.setTextSize(50);
		if( this.tile == null ) return false;
		float widthToSubstract = String.valueOf(this.tile.getAmount()).length() > 1 ? 30 : 15 ;
		canvas.drawText( String.valueOf( this.tile.getAmount() ), this.drawableRect.centerX() - widthToSubstract, this.drawableRect.centerY() + 15, paint);
		return true;
	}

	@Override
	public boolean onTouch(MotionEvent event) {
		return false;
	}
		
}
